﻿/*****************************************************************
// Copyright (C)
// All rights reserved.
// 
// Author:       WYQ
// Create Date:  2012-8-18 15:15:58
// Usage: 持久化数据库仓库
*****************************************************************/
using System;
using System.Linq;
using System.Linq.Expressions;

namespace Portal.Ef.Repository
{

   
    public class  AlbumRepository : Repository<Album, Album, TbAlbum>, IAlbumRepository<Album>
    {
        public override Album CreateEntity()
        {
            return new Album { };
        }
        public override IQueryable<Album> Get()
        {
            return MapQueryable(Entities.TbAlbum);
        }
        public override long Count()
        {
            return MapQueryable(Entities.TbAlbum).Count();
        }
        public override IQueryable<Album> Get(Expression<Func<Album, bool>> expression)
        {
            return MapQueryable(Entities.TbAlbum).Where(expression);
        }
        public override IQueryable<Album> GetSort(string sort = "Id", string order = "DESC")
        {
            Check.Argument.IsNotNull(sort, "排序字段");
            Check.Argument.IsNotNull(order, "排序条件");
            return LinqHelper.DataSorting(MapQueryable(Entities.TbAlbum), sort, order);
        }
        public override IQueryable<Album> GetSort(Expression<Func<Album, bool>> expression, string sort = "Id", string order = "DESC")
        {
            Check.Argument.IsNotNull(expression, "查询条件");
            Check.Argument.IsNotNull(sort, "排序字段");
            Check.Argument.IsNotNull(order, "排序条件");
            return LinqHelper.DataSorting(MapQueryable(Entities.TbAlbum).Where(expression), sort, order);
        }
        public override void Add(Album t)
        {
            Entities.AddToTbAlbum(ReverseMapModel(t));
        }
        public override void Update(Album t)
        {
            throw new NotImplementedException();
        }
        public override void AddOrUpdate(Album t)
        {
            if (Entities.TbAlbum.Any(o => o.CoAlbumId == t.Id))
            {

            }
            else
            {
                Entities.AddToTbAlbum(ReverseMapModel(t));
            }
        }
        public override void Delete(Album t)
        {
            Entities.TbAlbum.Where(o => o.CoAlbumId == t.Id).ForEach(o => Entities.DeleteObject(o));
        }
        public override void Delete(Expression<Func<Album, bool>> expression)
        {
            MapQueryable(Entities.TbAlbum).Where(expression).ForEach(o => Entities.DeleteObject(o));
        }
        public override void Save()
        {
            Entities.SaveChanges();
        }
       
        public JqEasyuiDataGrid<Album> JqEasyuiDataGrid(string code, string name, string categoryId, string sort = "CreateTime", string order = "Desc", int page = 10, int index = 1)
        {
            return new JqEasyuiDataGrid<Album>();
        }
      
        public override sealed IQueryable<Album> MapQueryable(IQueryable<TbAlbum> queryable)
        {
            return queryable.Select(o => new Album 
            {
         
	           Id=o.CoAlbumId,
	           Name = o.CoName 
,
	           CreateTime = o.CoCreateTime??DateTime.Now 
,
	           IsDelete = o.CoIsDelete??false 
,
	           Version = o.CoVersion??0 
,
	           IsClosed = o.CoIsClosed??false 
,
	           Culture = o.CoCulture 
,
	           Sort = o.CoSort??0 
,
	           CoverImage = o.CoCoverImage 
,
	           Description = o.CoDescription 
            
            });
        }
      
        public override sealed IQueryable<TbAlbum> ReverseMapQueryable(IQueryable<Album> queryable)
        {
            return queryable.Select(o => new TbAlbum 
            { 
        
	           CoAlbumId=o.Id,
	           CoName = o.Name
,
	           CoCreateTime = o.CreateTime
,
	           CoIsDelete = o.IsDelete
,
	           CoVersion = o.Version
,
	           CoIsClosed = o.IsClosed
,
	           CoCulture = o.Culture
,
	           CoSort = o.Sort
,
	           CoCoverImage = o.CoverImage
,
	           CoDescription = o.Description
  
            
            });
        }
      
        public override sealed Album MapModel(TbAlbum model)
        {
            return new Album {
	           Id=model.CoAlbumId,
	           Name = model.CoName
,
	           CreateTime = model.CoCreateTime??DateTime.Now
,
	           IsDelete = model.CoIsDelete??false
,
	           Version = model.CoVersion??0
,
	           IsClosed = model.CoIsClosed??false
,
	           Culture = model.CoCulture
,
	           Sort = model.CoSort??0
,
	           CoverImage = model.CoCoverImage
,
	           Description = model.CoDescription
            };
        }
        
        public override sealed TbAlbum ReverseMapModel(Album model)
        {
            return new TbAlbum { 
	           CoAlbumId=model.Id,
	           CoName = model.Name
	        
,
	           CoCreateTime = model.CreateTime
	        
,
	           CoIsDelete = model.IsDelete
	        
,
	           CoVersion = model.Version
	        
,
	           CoIsClosed = model.IsClosed
	        
,
	           CoCulture = model.Culture
	        
,
	           CoSort = model.Sort
	        
,
	           CoCoverImage = model.CoverImage
	        
,
	           CoDescription = model.Description
	        
            
            
            };
        }


    }
}



